<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <body>
    <div id="example">
      <c-component
        :show.sync="valueChild"
        style="padding: 30px 20px 30px 5px;border:1px solid #ddd;margin-bottom: 10px;"
      ></c-component>
      <button @click="changeValue">toggle</button>
    </div>
  </body>
</html>

<script src="./vue.js"></script>
<script>
  Vue.component('c-component', {
    template: `<div v-if="show">
                  <p>默认初始值是{{show}}，所以是显示的</p>
                  <button @click.stop="closeDiv">关闭</button>
               </div>`,
    props: ['show'],
    methods: {
      closeDiv() {
        this.$emit('update:show', false) //触发 input 事件，并传入新值
      }
    }
  })

  //vm实例
  var vm = new Vue({
    el: '#example',
    data: {
      valueChild: true
    },
    methods: {
      changeValue() {
        this.valueChild = !this.valueChild
      }
    }
  })
</script>
